package org.kimoji.security.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import org.kimoji.base.BaseController;
import org.kimoji.entity.biz.JsonRes;
import org.kimoji.security.entity.Account;
import org.kimoji.security.service.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SecurityController extends BaseController{

	@Autowired
	private SecurityService securityService;
	
	/**
	 * 跳轉至登錄頁
	 * <strong>Description</strong>:
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="login")
	public String showLogin(){
		return "security/login";
	}
	
	/**
	 * 跳轉至註冊頁
	 * <strong>Description</strong>:
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="register")
	public String showRegister(){
		return "security/register";
	}
	
	
	
	/**
	 * 登錄
	 * <strong>Description</strong>:
	 * @return
	 * @return Object
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="security/login")
	@ResponseBody
	public JsonRes login(Account account,HttpSession session,HttpServletRequest request,HttpServletResponse response){
		JsonRes jsonRes = securityService.login(account);
		auth((String)jsonRes.getAddition(), session, response);
		return jsonRes;
	}
	
	/**
	 * 註冊
	 * <strong>Description</strong>:
	 * @param account
	 * @return
	 * @return Object
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="security/register")
	@ResponseBody
	public JsonRes register(Account account,HttpSession session,HttpServletResponse response,String recaptcha_challenge_field,String recaptcha_response_field){
		JsonRes jsonRes = securityService.register(account,recaptcha_challenge_field,recaptcha_response_field);
		if(jsonRes.isSuccess()){
			auth((String)jsonRes.getAddition(), session, response);
		}
		return jsonRes;
	}
	
	/**
	 * 跳轉至更改密碼頁
	 * <strong>Description</strong>:
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="changepassword")
	public String changePassword(){
		return "security/changepassword";
	}
	
	/**
	 * 更改密碼
	 * <strong>Description</strong>:
	 * @param password
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="security/changepassword")
	@ResponseBody
	public String changePassword(Account account,String newPassowrd ,String retypeNewPassword){
		
		return null;
	}
	
	/**
	 * 退出
	 * <strong>Description</strong>:
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="exit")
	public String exit(@CookieValue(value="token")Cookie token,HttpServletResponse response){
		securityService.logout(token);
		token.setMaxAge(0);
		token.setValue(null);
		response.addCookie(token);
		return "redirect:index";
	}
	
	/**
	 * 忘記密碼
	 * <strong>Description</strong>:
	 * @return
	 * @return String
	 * @author chok
	 * 2013-12-15
	 */
	@RequestMapping(value="forgetPassword")
	public String forgetPassword(){
		return "security/forgetPassword";
	}
	
}
